package ar.edu.unicen.exa.isistan.archassis.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;

public class ListComposite extends Composite {
	protected Composite child = null;
	private ScrolledComposite sc;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ListComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.VERTICAL));
		
		// Create the ScrolledComposite to scroll horizontally and vertically
	    sc = new ScrolledComposite(this, SWT.V_SCROLL|SWT.H_SCROLL);

	    // Expand both horizontally and vertically
	    sc.setExpandHorizontal(true);
	    sc.setExpandVertical(true);
	    
	    // Create a child composite to hold the controls
	    child = new Composite(sc, SWT.NONE);
	    child.setLayout(new FormLayout());
	    
	   
	    

	    // Create the buttons
	    //new Button(child, SWT.PUSH).setText("One");
	    //new Button(child, SWT.PUSH).setText("Two");
	    /*
	     * // Set the absolute size of the child child.setSize(400, 400);
	     */
	    // Set the child as the scrolled content of the ScrolledComposite
	    

	    // Set the minimum size
	    //sc.setMinSize(8400, 8400);
	    
	    child.addListener(SWT.Resize, new Listener() {
	        int width = -1;
	        public void handleEvent(Event e) {
	          int newWidth = child.getSize().x;
	          if (newWidth != width) {
	        	  sc.setMinHeight(child.computeSize(newWidth, SWT.DEFAULT).y);
	            width = newWidth;
	          }
	        }
	      });
	    sc.setContent(child);
	    
	}
	protected void endChange() {
//		this.getSc().layout(true);
//		this.getSc().pack(true);
		sc.setContent(child);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public ScrolledComposite getSc() {
		return sc;
	}
	protected void noChildrens() {
		Label lblNewLabel = new Label(child, SWT.NONE);
	    FormData fd_lblNewLabel = new FormData();
	    fd_lblNewLabel.top = new FormAttachment(0, 0);
	    fd_lblNewLabel.left = new FormAttachment(0, 0);
	    lblNewLabel.setLayoutData(fd_lblNewLabel);
	    lblNewLabel.setText("No hay lista de clases");
	    child.layout();
	}
	protected void disposeChildrens() {
		Control[] childrens = child.getChildren();
		for (Control c : childrens) {
			c.dispose();
		}
	}
}
